#include <stdio.h>
#include <memory.h>
#include <conio.h>
#include <time.h>
#include <string>
#include <stdlib.h>



#define row 14
#define column 35
#define sample_n 1000
#define size_r 14*35
#define feature_n 177660

int main()
{
	int *sample;		//[sample_n][row*column]
	int out[sample_n];
	int g_temp[size_r];
	int i,j,ii,jj,k,l;

    time_t Start, End;
	double TimeUsed;
	
    FILE *infile, *outfile;
	char ctemp[10];
	sample = (int*)malloc(sample_n*row*column*sizeof(int));

	time ( &Start );
	//read all sample integral graph into sample
	for ( i = 0; i<500;i++){
	char file[100] ="C:\\MATLAB7\\project\\neg-";
	itoa(i,ctemp,10);
	strcat(file, ctemp);
	strcat(file,".pgm");
	for (j =0;j<3-strlen(ctemp);j++)
		strcat(file, " ");
	strcat(file, ".bin");
	infile = fopen(file ,"rb");
	fread((sample+i*size_r) , sizeof(int), size_r, infile);
	memcpy(out,sample+i*size_r,100*sizeof(int));
	fclose(infile);
	}
	for ( i = 0; i<500;i++){
	char file[100] ="C:\\MATLAB7\\project\\pos-";
	itoa(i,ctemp,10);
	strcat(file, ctemp);
	strcat(file,".pgm");
	for (j =0;j<3-strlen(ctemp);j++)
		strcat(file, " ");
	strcat(file, ".bin");
	infile = fopen(file ,"rb");
	fread((sample+(i+500)*size_r) , sizeof(int), size_r, infile);
	// memcpy(out,sample+(i+500)*size_r,100*sizeof(int));     //only for test
	fclose(infile);
	}

	// end reading data
	infile = fopen("h_out.bin","rb");
	outfile = fopen("f_out.bin","wb");

    for (k =0; k< feature_n; k++)
	{
		fread(g_temp,sizeof(int),size_r,infile);
		memset(out,0,sizeof(out));
		for (i = 0 ; i<sample_n ;i++){
			for (j = 0; j< size_r; j++){
				switch(g_temp[j]){
					case 1:
						out[i] += *(sample + i*size_r +j) ;
						break;
					case -1:
						out[i] -= *(sample + i*size_r +j) ;
					default:
						break;
				}				
			}
		}
		fwrite(out,sizeof(int),sample_n,outfile);

	}//end for (features)
	time ( &End );
	TimeUsed = difftime( End, Start ); 
	printf("total time is %8.11f seconds\n", TimeUsed);


}

